using UnityEditor;
using UnityEngine;

namespace Framework
{
    public class ShaderEditor : EditorWindow
    {
        public static void CreateWindow()
        {
            ShaderEditor bundleBuilder = GetWindow<ShaderEditor>(true, "shader关键字过滤", true);
            bundleBuilder.minSize = new Vector2(800, 500);
            bundleBuilder.maxSize = new Vector2(800, 800);
        }
        private SerializedObject serializedObject;
        private ShaderConfig m_config = null;
        private Vector2 scrollViewPos = Vector2.zero;
        private void OnEnable()
        {
            m_config = ShaderConfig.GetInstance();
        }
        private void OnDisable()
        {
            m_config?.Save();
        }
        private void OnGUI()
        {

            EditorGUILayout.LabelField("实现 IPreprocessShaders 接口,把没配制的关键字禁用,或移除全部", EditorStyles.boldLabel);
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("收集", GUILayout.MinWidth(80), GUILayout.Height(80)))
            {
                m_config = ShaderConfig.GetInstance();
                if (m_config != null)
                {
                    var shaderGuidList = AssetDatabase.FindAssets("t:Shader");
                    foreach (var shaderGuid in shaderGuidList)
                    {
                        string assetPath = AssetDatabase.GUIDToAssetPath(shaderGuid);
                        var shader = AssetDatabase.LoadAssetAtPath<Shader>(assetPath);
                        m_config.AddShader(shader);
                    }
                    m_config.Update();
                }
                else
                {
                    Debug.LogError("没有配制文件");
                }

            }
            if (GUILayout.Button("保存", GUILayout.MinWidth(80), GUILayout.Height(80)))
            {
                m_config = ShaderConfig.GetInstance();
                if (m_config != null)
                {
                    m_config.Save();
                }
                else
                {
                    Debug.LogError("没有配制文件");
                }

            }
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();
            if (serializedObject == null && m_config != null)
            {
                serializedObject = new SerializedObject(m_config);
            }
            if (serializedObject == null) return;
            serializedObject.Update();
            scrollViewPos = GUILayout.BeginScrollView(scrollViewPos, false, false);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("Shaders"));
            GUILayout.EndScrollView();
            serializedObject.ApplyModifiedProperties();
        }
    }
}